﻿Ext.define('app.prm.document.Edit',
{
    extend: 'Ext.window.Window',
    title: '上传文档',
    autoScroll: true,
    modal: true,
    width: 600,
    autoHeight: true,
    layout: 'fit',
    documentId: 0,
    initComponent: function () {
        var me = this;
        me.items = [this.getUploadForm()];
        me.buttons = [
            { text: '保存', scope: this, handler: me.onSave },
            { text: '关闭', handler: function () { me.close(); } }
        ]
        me.callParent();
    },
    listeners: {
        afterrender: function (me) {
            App.AjaxGet('/api/services/app/document/Get?documentId=' + me.documentId, function (data)
            {
                data.result.reportDate = data.result.reportDate.substring(0, 10)
                me.uploadForm.getForm().setValues(data.result);
            });
        }
    },
    getUploadForm: function () {
        var me = this;
        if (!this.uploadForm) {
            this.uploadForm = new Ext.form.Panel({
                defaultType: 'textfield',
                border: false,
                defaults: { anchor: '100%' },
                fieldDefaults: {
                    labelWidth: 90,
                    labelAlign: "right",
                    flex: 1,
                    margin: 6
                },
                items: [
                    {
                        xtype: 'filefield', fieldLabel: '文档附件', name: 'ProjectFile', buttonText: '浏览',
                        listeners: {
                            change: function (fld, value) {
                                var filename = value.replace(/(^.*(\\|\/))?/, "");
                                me.uploadForm.query("textfield[name=name]")[0].setValue(filename);
                            }
                        }
                    },
                    { fieldLabel: '文档名称', name: 'name' },
                    
                     {   xtype: 'datefield',
                        name: 'reportDate',
                        fieldLabel: '报告时间',
                        value: new Date(),
                        format: 'Y-m-d'
                    },
                    {
                        xtype: 'textarea',
                        name: 'note',
                        fieldLabel: '备注说明',
                        height: 200
                    }
                ]
            });
        }
        return this.uploadForm;
    },
    onSave: function ()
    {
        var me = this;
        me.uploadForm.getForm().submit({
            url: '/api/services/app/document/EditDocument',
            params: { DocumentId: me.documentId },
            success: function (form, response)
            {
                me.fireEvent("savesuccess");
            }
        });
    }
});